<?php


namespace App\HttpController\Internet;


use App\Model\TransportModel;
use EasySwoole\Validate\Validate;

class Transport extends \App\HttpController\BaseInternet
{
    //土质类型
    protected $soil_type = [
        1 => '砂石土',
        2 => '渣土',
        3 => '碎石块',
        4 => '砖瓦碎块',
        5 => '其他',
    ];
    //土质类型id转名
    protected function soilIdtoName($id){
        if ($id == "1") {
            $soilName = '砂石土';
        } elseif ($id == "2") {
            $soilName = '渣土';
        } elseif ($id == "3") {
            $soilName = '碎石块';
        } elseif ($id == "3") {
            $soilName = '砖瓦碎块';
        } else {
            $soilName = '其他';
        }
        return $soilName;
    }
    //运单状态
    protected $waybill_status = [
        0 => '待运输',
        1 => '补录',
        2 => '进行中',
        3 => '已完成',
        4 => '失效',
    ];
    //参数校验
    protected function validateRule(?string $action): ?Validate
    {
        $v = new Validate();
        switch ($action) {

            case'getWaybillList':
                $v->addColumn('waybill_number', '运单号')->required('参数不能缺少');
                $v->addColumn('vehicle_number', '车牌号')->required('参数不能缺少');
                $v->addColumn('waybill_status_ids', '运单状态')->required('参数不能缺少');
                $v->addColumn('waybill_type_ids', '运单类型')->required('参数不能缺少');
                $v->addColumn('start_point', '起点')->required('参数不能缺少');
                $v->addColumn('end_point', '终点')->required('参数不能缺少');
                $v->addColumn('driver', '司机')->required('参数不能缺少');
                $v->addColumn('soil_type_id', '土质类型')->required('参数不能缺少');
                $v->addColumn('tab', '切换')->required('参数不能缺少');
                $v->addColumn('start_time1', '开始时间1')->required('参数不能缺少');
                $v->addColumn('end_time1', '结束时间1')->required('参数不能缺少');
                $v->addColumn('start_time2', '开始时间2')->required('参数不能缺少');
                $v->addColumn('end_time2', '结束时间2')->required('参数不能缺少');
                $v->addColumn('sort_key', '排序关键字')->required('参数不能缺少');
                $v->addColumn('sort_type', '排序类型')->required('参数不能缺少');
                $v->addColumn('pn', '页码')->required('参数不能缺少')->min(1);
                $v->addColumn('page_size', '每页条数')->required('参数不能缺少')->min(1);
                $v->addColumn('belong_pound', '关联磅单')->required('参数不能缺少');
                break;
        }
        return $v;
    }
    //获取土质类型
    public function getSoilType(){
        $data = [];
        foreach ($this->soil_type as $key => $value) {
            $data[] = ['value' => (string)$key, 'label' => $value];
        }
        return $this->returnMsg($data, 20000, '请求成功！');
    }
    //获取运单管理列表
    public function getWaybillList()
    {
        $field = [
            'a.id',
            'a.order_id',
            'a.soil_type_name',
            'a.start_address',
            'a.end_address',
            'a.license_plate',
            'a.driver_name',
            'a.start_time',
            'a.end_time',
            'a.status',
            'a.total_price',
            'a.estimate_volume',
            'a.enter_weighbridge',
            'a.is_deviation',
            'a.is_destroy',
            'a.is_company',
            'b.suttle',
        ];
        $params = $this->paramData;
        if ($params != array()) {
            foreach ($params as $k => $par_v) {
                $params[$k] = trim($par_v);
            }
        }
        $par = [];
        $par['tab'] = $params["tab"];
        $par['pn'] = $params["pn"];
        $par['page_size'] = $params["page_size"];
        $par['waybill_number'] = $params["waybill_number"];
        $par['vehicle_number'] = $params["vehicle_number"];
        $par['waybill_status_ids'] = $params["waybill_status_ids"];
        $par['waybill_type_ids'] = $params["waybill_type_ids"];
        $par['start_point'] = $params["start_point"];
        $par['end_point'] = $params["end_point"];
        $par['driver'] = $params["driver"];
        $par['soil_type_id'] = $params["soil_type_id"];
        $par['start_time1'] = $params["start_time1"];
        $par['start_time2'] = $params["start_time2"];
        $par['end_time1'] = $params["end_time1"];
        $par['end_time2'] = $params["end_time2"];
        $par['sort_key'] = $params["sort_key"];
        $par['sort_type'] = $params["sort_type"];
        $par['belong_pound'] = $params["belong_pound"];

        $get_waybill_title = TransportModel::create()->getWaybillTitle();
        $get_waybill_list = TransportModel::create()->getWaybillList($par, $field);

        $list = [];
        foreach ($get_waybill_list['data'] as $k => $value) {
            $result = [];
            $result['id'] = $value['id'];
            $result['order_id'] = $value['order_id'];
            //土质类型
            $result['soil_type_name'] = $this->soilIdtoName($value['soil_type_name']);
            $result['start_address'] = $value['start_address'];
            $result['end_address'] = $value['end_address'];
            $result['license_plate'] = $value['license_plate'];
            $result['driver_name'] = $value['driver_name'];
            $result['start_time'] = $value['start_time'];
            $result['end_time'] = $value['end_time'];
            //运单状态
            if ($value['status'] == '0') {
                $result['waybill_status_text'] = '待运输';
            } elseif ($value['status'] == '1') {
                $result['waybill_status_text'] = '补录';
            } elseif ($value['status'] == '2') {
                $result['waybill_status_text'] = '进行中';
            } elseif ($value['status'] == '3') {
                $result['waybill_status_text'] = '已完成';
            } elseif ($value['status'] == '4') {
                $result['waybill_status_text'] = '失效';
            }
            $result['total_price'] = $value['total_price'];
            $result['estimate_volume'] = $value['estimate_volume'];
            $result['enter_weighbridge'] = $value['enter_weighbridge'];
            $result['is_deviation'] = $value['is_deviation'];
            $result['weighbridge'] = $value['suttle'];
            $result['is_destroy'] = $value['is_destroy'];
            $result['is_company'] = $value['is_company'];
            $list[] = $result;
        }

        //print_r(json_encode($amount));
        //print_r(json_encode($list));
        //返回数据给前端
        $other = [];
        $other['total'] = $get_waybill_list['total'];
        $other['amount'] = $get_waybill_title;
//        print_r("返回total总数：" . $other['total']);
//        echo PHP_EOL;
//        print_r("运行成功！！！");
//        echo PHP_EOL;
//        print_r($list);
        return $this->returnMsg($list, 20000, 'data返回成功', $other);
    }
    //获取运单管理详情
    public function getWaybillInfo()
    {
        echo "运单详情 Start!!!";
        echo PHP_EOL;
        $param = $this->paramData;
        $par = [
            'id'=> $param['id']
        ];
        $field = [
                'c.id','c.waybill_id','c.tel','c.transport_company_name','c.use_company_name','c.ts','c.pound_img_list',

                'a.driver_name','a.license_plate','a.start_time','a.end_time','a.soil_type_name','a.weighbridge','a.estimate_volume',
                'a.enter_weighbridge','a.end_time','a.estimated_arrival_time',

                'a.waybill_type_ids','a.is_destroy','a.status','a.start_address','a.end_address','a.start_time','a.end_time',
                'a.total_price','a.load_square'
            ];
//        $abnormal = [
//            'weighbridge','enter_weighbridge','end_time','estimated_arrival_time'
//        ];

        //获取运单信息
        $get_waybill_detail = TransportModel::create()->getWaybillDetail($par,$field);

        /*$get_waybill_detail[0]['start_time'],
        $get_waybill_detail[0]['end_time'],
        $get_waybill_detail[0]['yj_time'],*/

        //判断是否超时
        //$get_waybill_detail[0]['zhongliang']
        //是否超重



        //获取异常信息
        $getOverTimeInfo = TransportModel::create()->getOverTimeInfo($par,$get_waybill_detail);

//        var_dump($get_waybill_detail[0]['pound_img_list']);
        //将返回的结果封装到数组中
        $order_info = [
            'id' => $get_waybill_detail[0]['id'],
            'status' => $get_waybill_detail[0]['status'],
            'is_destroy' => $get_waybill_detail[0]['is_destroy'],
            'waybill_number' => $get_waybill_detail[0]['waybill_id'],
            'start_time' => $get_waybill_detail[0]['start_time'],
            'end_time' => $get_waybill_detail[0]['end_time'],
            'start_point' => $get_waybill_detail[0]['start_address'],
            'end_point' => $get_waybill_detail[0]['end_address'],
            'driver' => $get_waybill_detail[0]['driver_name'],
            'tel' => $get_waybill_detail[0]['tel'],
            'transport_company_name' => $get_waybill_detail[0]['transport_company_name'],
            'plate_number' => $get_waybill_detail[0]['license_plate'],
            'use_company_name' => $get_waybill_detail[0]['use_company_name'],
            'ts' => $get_waybill_detail[0]['ts'],
            //土质类型id转土质类型名
//            'soil_type_name' => $get_waybill_detail[0]['soil_type_name'],
            'soil_type_name' => $this->soilIdtoName($get_waybill_detail[0]['soil_type_name']),
            'net_weight' => $get_waybill_detail[0]['weighbridge'],
            'estimate_square' => $get_waybill_detail[0]['estimate_volume'],
            'total' => $get_waybill_detail[0]['total_price'],
            //磅单凭证
            'pound_img_list' => TransportModel::create()->poundArray($get_waybill_detail[0]['pound_img_list'])
        ];

        $over_time_info = $getOverTimeInfo;
        $event_feedback = [];
        $belong_pound =[];
        $log = [];
        $other = [];
        $data = [
            'order_info' => $order_info,
            'over_time_info' => $over_time_info,
            'event_feedback' => $event_feedback,
            'belong_pound' => $belong_pound,
            'log' => $log,
            'other' => $other
        ];
        print_r('运单详情 Success!!!');
        return $this->returnMsg($data, 20000, '运单信息返回成功');
    }
    //销单
    public function destroyWaybill(){
        $param = $this->paramData;
        $par = [
            'id'=> $param['ids']
        ];
        $destroy = ['is_destroy'];
        $destroyWaybill = TransportModel::create()->destroyWaybill($par,$destroy);

        return $this->returnMsg([],20000,'销单完成');
    }
}
